/*
 * Created by JFormDesigner on Mon Jun 21 13:56:02 GMT+08:00 2021
 */

package com.bk.view;

import java.awt.event.*;

import com.bk.dao.BookDao;
import com.bk.dao.BookTypeDao;
import com.bk.util.ToolUtil;

import java.awt.*;
import java.sql.SQLException;
import java.util.List;
import javax.swing.*;
import javax.swing.GroupLayout;

/**
 * @author Ayi
 */
// 添加图书窗口类
public class addBookFrame extends JFrame {
    public addBookFrame() {
        initComponents();
        try {
            setBookType();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    // 初始化下拉列表项到窗口供选择（从数据库获取）
    public void setBookType() throws SQLException, ClassNotFoundException {
        List<String> booktype = new BookTypeDao().getAllBookType();
        for (String x : booktype) {
            comboBox1.addItem(x);
        }
    }

    // 添加图书，监听
    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        String bname = textField1.getText();
        if (new ToolUtil().judgeIdOrName(bname.trim())) {
            JOptionPane.showMessageDialog(null, "书名不可以为纯数字！");
            return;
        }
        int binventory = (int)spinner1.getValue();
        String btype = comboBox1.getSelectedItem().toString();
        try {
            if (new BookDao().addBook(bname, binventory, btype)) {
                JOptionPane.showMessageDialog(null, "添加成功");
                dispose();
            }else {
                JOptionPane.showMessageDialog(null, "添加失败，请检查书名为空或已存在");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    private void textField1KeyTyped(KeyEvent e) {
        // TODO add your code here
        // 文本框监听，限制输入长度
        String s = textField1.getText();
        if (s.length() > 7) {
            e.consume();
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - unknown
        label1 = new JLabel();
        textField1 = new JTextField();
        label2 = new JLabel();
        spinner1 = new JSpinner();
        label3 = new JLabel();
        comboBox1 = new JComboBox();
        button1 = new JButton();

        //======== this ========
        setTitle("\u4e0a\u67b6\u56fe\u4e66");
        setResizable(false);
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u4e66\u540d");
        label1.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));

        //---- textField1 ----
        textField1.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                textField1KeyTyped(e);
            }
        });

        //---- label2 ----
        label2.setText("\u4f59\u91cf");
        label2.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));

        //---- spinner1 ----
        spinner1.setBackground(Color.white);
        spinner1.setFont(spinner1.getFont().deriveFont(spinner1.getFont().getSize() + 2f));
        spinner1.setModel(new SpinnerNumberModel(1, 1, null, 1));

        //---- label3 ----
        label3.setText("\u7c7b\u578b");
        label3.setFont(new Font("\u5fae\u8f6f\u96c5\u9ed1", Font.PLAIN, 14));

        //---- button1 ----
        button1.setText("\u786e\u5b9a\u6dfb\u52a0");
        button1.setBackground(Color.white);
        button1.addActionListener(e -> button1ActionPerformed(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                    .addGap(42, 42, 42)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                        .addComponent(button1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(label1, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
                                .addComponent(label2, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
                                .addComponent(label3, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE))
                            .addGap(26, 26, 26)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(textField1)
                                .addGroup(contentPaneLayout.createSequentialGroup()
                                    .addComponent(spinner1, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
                                    .addGap(0, 0, Short.MAX_VALUE))
                                .addComponent(comboBox1))))
                    .addGap(62, 62, 62))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(28, 28, 28)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label1))
                    .addGap(28, 28, 28)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label2)
                        .addComponent(spinner1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(25, 25, 25)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(comboBox1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label3))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                    .addComponent(button1)
                    .addGap(29, 29, 29))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - unknown
    private JLabel label1;
    private JTextField textField1;
    private JLabel label2;
    private JSpinner spinner1;
    private JLabel label3;
    private JComboBox comboBox1;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
